// this program processes the file "userpass.txt".

// does hashing by creating a hash table by userid.

// hash fucntion used is: a type of multiplcation hash function
//multiplied ascii value of first charcter of username with 0.23.

// best case: when all the userid's start with different characters.

//worst case:when all the userid's start with same characters.

// average case :when some have same starting character.

//writing to a file has been in the commented mode to write the file on your own
// please decomment lines from 83-109.(IMPORTANT PLZ READ)

//the text file I created will be attached in the mail.


#include<stdio.h>
#include<string.h>
struct node				// tok a array of structure to store username and
{						//password in the hash table.
	char user[40];
	char pass[20];
	
}n[30];					//size of hash table is 30, hence maximum 
						//posssible enteries can be 30 as 
						//required by open addressing scheme.(alpha<1)
int h=0;
void search()	//to search for the password of the userid and to verify other 
				//things.
{
	int q,j,p;
	int i=0,k=0,flag=0;
	char password[20];
	char userid[40];
	printf("\nplease enter the userid max length is 40\n");
	scanf("%s",&userid);
	q=userid[0];//getting the ascii value of first character of the entered username
	p=(0.23)*q;//as highest value of q is 128 highest value for p 
	            //will come out to be 29 as reqired.
	do 
	{
		j=p+i;//hashfunction to find an entry from the hash table.
		if(strcmp(n[j].user,userid)==0)//checking whether userid entered is valid.
		{
			flag=1;		//  variable set to 1 when userid matches
			while(k<3)	//allowing maximum tries to be 3.
			{
				printf("enter the password\n");
				scanf("%s",password);
				if(strcmp(n[j].pass,password)==0)
				{
					printf("yor account has been opened\n");
					h=1;
					break;
					//k=5;	//just an aribitary value to get out of the loop
				}
				else
				{
					printf("you entered the wrong password\n");
					k++;
					if(k==3)		//too many tries access denied
					{
						printf("you cannot open ur account\n");
						h=1;
					}	
				}
				
				
			}
		}
		else
		{
			i++;
		}	
	}while(i!=30 && strcmp(n[j].user,"")!=0&&h!=1);/*loop continues till i!=30 and 
											 userid block inthe hash table
											 is not empty*/
	if(flag==0)								//if userid does not match
	{
		printf("the userid yo enterd could not be found");
	}
	/*if(k==3)
	{
		printf("sorry u cannot open the account"); 
	}*/
	
}
void main()
{

	int x,i,m,l,y;
	FILE *fp,*fp1;
	char ch='y';
	char user[40];
	char pass[20];
	char userid[40];
	
	/*struct rec		// structure to write username and password in 
						//the text file. 
	{
		char user[40];
		char pass[20];
	};
	struct rec a;
	fp=fopen("userpass.txt","w");	//openig file in write mode.
	if(fp==NULL)
	{
		
		puts("cannot open file");
		exit(1);
	}
	// writing the file and prompting user to add another record or not.
	while(ch=='y'||ch=='Y')
	{
		
		printf("\nenter the username and password seperated by space");
		scanf("%s%s",&a.user,&a.pass);
		fprintf(fp,"%s %s\n",a.user,a.pass);
		printf("do you want to ad another username password(y/n)");
		fflush(stdin);
		scanf("%c",&ch);
		
	}
	fclose(fp);*/
	//struct rec b;
	fp1=fopen("userpass.txt","r");//opening the file inthe read mode
	if(fp==NULL)
	{
		
		puts("cannot open file");
		exit(1);
	}
	while(fscanf(fp1,"%s %s",user,pass)!=EOF)//scanning the recods from the file
											// till we encounter end of file
	{
		printf("\n%s %s\n",user,pass);//printing the userid and password
		l=user[0];					//getting first character of username
		printf("ascii value of 1st character:%d\n",l);
		i=0;
		while(i!=30)	//loop tiil 30 for probing at max 30 times.
		{
			x=user[0];
			y=(0.23)*x;//multiplying the ascii value of first letter of username 
						//by0.23 because maxascii value can be 128and 128*(0.23)=29.44
			m=y+i;
			if(strcmp(n[m].user,"")==0)
			{
				//p[m]=&n[m];
				strcpy(n[m].user,user);
				strcpy(n[m].pass,pass);
				//printf("\n%s %s",n[m].user,n[m].pass);
				printf("index value in hash table is:%d\n",m);//printing the index of the hsh table.
				break;	
			}
			else
			{
				i++;
			}
		
		}
	}
	fclose(fp1);//closing the file using file pointer.
	
	
	search();//calling the search function to do the further process.
		
	
	
}